﻿using System.Collections.Generic;
using System.Web.Mvc;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System.Data;
using ChestPainCenterSystem.Bll;
using ChestPainCenterSystem.ThreeCenterManage;
using System.Net.Http;
using System.Text;
using System.IO;
using ChestPainCenterSystem.Core;
using System.Web.Http;
using ChestPainCenterSystem.MedicalConfig;

namespace ChestPainCenterSystem
{
    public class BaseController : Controller
    {
        public static ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();

        /// <summary>
        /// 登录用户信息
        /// </summary>
        public SysLoginUser loginUser
        {
            get
            {
                return LoginManager.GetLoginUser();
            }
        }
        /// <summary>
        /// 允许使用get方法调用json
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult JSON(object data)
        {
            return base.Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetLoginUserInfo()
        {
            return JsonDate(loginUser);
        }

        /// <summary>
        /// 返回处理过的时间的Json字符串
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public ContentResult JsonDate(object date)
        {
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            return Content(JsonConvert.SerializeObject(date, Formatting.Indented, timeConverter));
        }

        public ContentResult JsonFilerNull(object date)
        {
            var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            return Content(JsonConvert.SerializeObject(date, Formatting.Indented, jSetting));
        }

        //public partial class PatientTrajectory
        //{
        //    public string TagID { get; set; }
        //    public string TagName { get; set; }
        //    public string PositionCode { get; set; }
        //    public string PositionName { get; set; }
        //    public string EventCode { get; set; }
        //    public string EventName { get; set; }
        //    public string EventTime { get; set; }
        //}

        [System.Web.Http.HttpPost]
        [LoginAttribute(IsNeed = false)]
        public HttpResponseMessage SetPatientTrajectory([FromBody] List<PatientTrajectory> data)
        {
            if (data == null) data = new List<PatientTrajectory>();
            PatientTrajectory[] patientTrajectoryArray = new PatientTrajectory[data.Count];
            for (int i = 0; i < data.Count; i++)
            {
                patientTrajectoryArray[i] = data[i];
            }
            var Data = service.SetPatientTrajectory(patientTrajectoryArray);
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(Data.D), Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }

        /// <summary>
        /// 获取病历列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMedicalList()
        {
            Medical_ConfigSoapClient service = new Medical_ConfigSoapClient();
            ChestPainCenterSystem.MedicalConfig.ResultHelperOfString serviceResult = service.GetBase_Medical("");
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                return JsonDate(jsonobj);
            }
            return JsonDate(null);
        }

        /// <summary>
        /// 获取省份列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetProvince()
        {
            Medical_ConfigSoapClient service = new Medical_ConfigSoapClient();
            ChestPainCenterSystem.MedicalConfig.ResultHelperOfString serviceResult = service.GetProvince();
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                return JsonDate(jsonobj);
            }
            return JsonDate(null);
        }

        /// <summary>
        /// 获取城市列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCity(string province)
        {
            Medical_ConfigSoapClient service = new Medical_ConfigSoapClient();
            ChestPainCenterSystem.MedicalConfig.ResultHelperOfString serviceResult = service.GetCity(province);
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                return JsonDate(jsonobj);
            }
            return JsonDate(null);
        }

        /// <summary>
        /// 获取地区列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDistrict(string city)
        {
            Medical_ConfigSoapClient service = new Medical_ConfigSoapClient();
            ChestPainCenterSystem.MedicalConfig.ResultHelperOfString serviceResult = service.GetDistrict(city);
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                return JsonDate(jsonobj);
            }
            return JsonDate(null);
        }
    }
}